package com.syni.mdd.sjb.community.base.controller.protect;

import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.common.mapper.DxshAppUserInfoMapper;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxAdmin;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxAdminRoleRelation;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxCompany;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxCompanyUser;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.sjb.common.datatreasure.DxshApiIndustryRestService;
import com.syni.mdd.sjb.community.base.annotion.AdminAuthVerification;
import com.syni.mdd.sjb.community.base.config.QywxProperties;
import com.syni.mdd.sjb.community.base.constants.Constants;
import com.syni.mdd.sjb.community.base.constants.ResultCode;
import com.syni.mdd.sjb.community.base.constants.ResultMsg;
import com.syni.mdd.sjb.community.base.entity.vo.BaseRqVo;
import com.syni.mdd.sjb.community.base.entity.vo.rp.businessman.AdminDataRpVo;
import com.syni.mdd.sjb.community.base.entity.vo.rp.businessman.CorpDataRpVo;
import com.syni.mdd.sjb.community.base.entity.vo.rq.AddAdminManVo;
import com.syni.mdd.sjb.community.base.entity.vo.rq.BindSjbAccountVo;
import com.syni.mdd.sjb.community.base.entity.vo.rq.DeleteAdminVo;
import com.syni.mdd.sjb.community.base.service.SjbQywxAdminRoleRelationService;
import com.syni.mdd.sjb.community.base.service.SjbQywxAdminService;
import com.syni.mdd.sjb.community.base.service.SjbQywxCompanyService;
import com.syni.mdd.sjb.community.base.service.SjbQywxCompanyUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业管理
 * **/
@RestController
@RequestMapping("/adminBusiness")
@Log4j2
public class AdminBusinessManController{

    @Autowired
    SjbQywxAdminService sjbQxwxAdminService;

    @Autowired
    SjbQywxAdminRoleRelationService sjbQywxAdminRoleRelationService;

    @Autowired
    SjbQywxCompanyUserService sjbQywxCompanyUserService;

    @Autowired
    SjbQywxCompanyService sjbQywxCompanyService;

    @Autowired
    DxshAppUserInfoMapper dxshAppUserInfoMapper;

    @Autowired
    DxshApiIndustryRestService dxshApiIndustryRestService;

    @Autowired
    QywxProperties qywxProperties;

    @AdminAuthVerification
    @PostMapping("/corpData")
    public Map<String, Object> corpData(@RequestBody BaseRqVo baseRqVo) {
        CorpDataRpVo corpDataRpVo = new CorpDataRpVo();
        SjbQywxCompany sjbQywxCompany = sjbQywxCompanyService.getByCompanyId(baseRqVo.getCorpid());
        if(sjbQywxCompany != null){
            corpDataRpVo.setCompanySimpleName(sjbQywxCompany.getCorpName());
            corpDataRpVo.setCompanyAddress(sjbQywxCompany.getLocation());
            corpDataRpVo.setCorpIndustry(sjbQywxCompany.getCorpIndustry());
            corpDataRpVo.setCompanyNum(sjbQywxCompanyUserService.countCompanyNum(sjbQywxCompany.getCorpId()));
            corpDataRpVo.setCorpId(sjbQywxCompany.getCorpId());
            SjbQywxAdmin sjbQywxAdmin = sjbQxwxAdminService.getBySuperAdminByCorpId(sjbQywxCompany.getCorpId());
            if(sjbQywxAdmin != null) {
                corpDataRpVo.setSuperAdmin(sjbQywxAdmin.getName());
                corpDataRpVo.setSuperAdminPhone(sjbQywxAdmin.getPhone());
                if (StringUtils.isNotBlank(corpDataRpVo.getSuperAdminPhone())) {
                    corpDataRpVo.setIsBindPhone(Constants.COMMON_FLAG_TRUE_INT);
                }
                if (sjbQywxAdmin.getSjbUserId() != null) {
                    DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoMapper.selectById(sjbQywxAdmin.getSjbUserId());
                    if(dxshAppUserInfo != null){
                        Map<String,Object> resultMap = dxshApiIndustryRestService.getUserChooseIndustry(dxshAppUserInfo.getUserId());
                        if(ResultMapHelper.isSuccess(resultMap)){
                            Map<String,Object> data = (Map<String, Object>) resultMap.get("data");
                            if(data.containsKey("industryName")){
                                corpDataRpVo.setIsChooseIndustry(Constants.COMMON_FLAG_TRUE_INT);
                                corpDataRpVo.setChooseIndustry(data.get("industryName").toString());
                            }
                        }
                    }
                }
            }
        }
        return ResultMapHelper.success(corpDataRpVo);
    }

    @PostMapping("/corpData1")
    public Map<String, Object> corpData1(@RequestParam("name") String name) {
        return ResultMapHelper.success();
    }

    @AdminAuthVerification
    @PostMapping("/manList")
    public Map<String, Object> manList(@RequestBody BaseRqVo baseRqVo) {
        return sjbQxwxAdminService.manList(baseRqVo);
    }

    @AdminAuthVerification
    @PostMapping("/addAdmin")
    public Map<String, Object> addAdminMan(@RequestBody AddAdminManVo addAdminManVo) {
        //添加一个管理员
        List<SjbQywxCompanyUser> companyUser = sjbQywxCompanyUserService.getByIdIn(addAdminManVo.getMerberIds());
        if(sjbQxwxAdminService.addCompanyUserToAdmins(companyUser)) {
            return ResultMapHelper.success(new HashMap<>());
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.HAND_FAIL);

    }

    @AdminAuthVerification
    @PostMapping("/adminData")
    public Map<String, Object> adminData(@RequestBody BaseRqVo baseRqVo) {
        AdminDataRpVo adminDataRpVo = new AdminDataRpVo();
        SjbQywxAdmin sjbQxwxAdmin = sjbQxwxAdminService.getById(baseRqVo.getQxwxAdminId());
        if(sjbQxwxAdmin == null){
            return ResultMapHelper.result(ResultCode.CODE_PC_NO_LOGIN, ResultMsg.MSG_PC_TOKEN_FAIL);
        }
        adminDataRpVo.setAvatar(sjbQxwxAdmin.getAvatar());
        adminDataRpVo.setName(sjbQxwxAdmin.getName());
        adminDataRpVo.setPhone(sjbQxwxAdmin.getPhone());
        if(StringUtils.isNotBlank(sjbQxwxAdmin.getPhone())){
            adminDataRpVo.setIsBindPhone(Constants.COMMON_FLAG_TRUE_INT);
        }
        SjbQywxAdminRoleRelation sjbQywxAdminRoleRelation = sjbQywxAdminRoleRelationService.getQywxAdminId(sjbQxwxAdmin.getId());
        if(sjbQywxAdminRoleRelation != null){
            adminDataRpVo.setAdminRole(sjbQywxAdminRoleRelation.getRoleId());
        }
        adminDataRpVo.setUserid(sjbQxwxAdmin.getUserid());
        adminDataRpVo.setSjbUserId(sjbQxwxAdmin.getSjbUserId());
        return ResultMapHelper.success(adminDataRpVo,ResultMsg.HAND_SUCCESS);
    }

    @AdminAuthVerification
    @PostMapping("/deleteAdmin")
    public Map<String, Object> deleteAdmin(@RequestBody DeleteAdminVo deleteAdminVo) {
        if(!sjbQxwxAdminService.deleteAdmin(deleteAdminVo.getAdminId())){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.HAND_FAIL);
        }
        return ResultMapHelper.success(new HashMap<>());
    }

    @AdminAuthVerification
    @PostMapping("/bindSjbAccount")
    public Map<String, Object> bindSjbAccount(@RequestBody BindSjbAccountVo bindSjbAccountVo) {
        return sjbQxwxAdminService.bindSjbAccount(bindSjbAccountVo);
    }

}
